package com.shxzz.shortlink.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shxzz.shortlink.admin.common.biz.user.UserContext;
import com.shxzz.shortlink.admin.common.convention.exception.ClientException;
import com.shxzz.shortlink.admin.common.convention.result.Result;
import com.shxzz.shortlink.admin.dao.entity.GroupDO;
import com.shxzz.shortlink.admin.dao.mapper.GroupMapper;
import com.shxzz.shortlink.admin.remote.LinkRemoteService;
import com.shxzz.shortlink.admin.remote.dto.req.RecycleBinPageReqDTO;
import com.shxzz.shortlink.admin.remote.dto.resp.RecycleBinLinkPageRespDTO;
import com.shxzz.shortlink.admin.service.RecycleBinService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author shxzz
 * @create 2023-12-25-20:21
 * @description 回收站管理接口实现类
 */
@Service
@RequiredArgsConstructor
public class RecycleBinServiceImpl implements RecycleBinService {

    private final GroupMapper groupMapper;

    /**
     * 后续重构为 SpringCloud Feign 调用
     */
    LinkRemoteService shortLinkRemoteService = new LinkRemoteService() {
    };
    @Override
    public Result<IPage<RecycleBinLinkPageRespDTO>> selectPageRecycleBin(RecycleBinPageReqDTO requireParam) {
        LambdaQueryWrapper<GroupDO> queryWrapper = Wrappers.lambdaQuery(GroupDO.class)
                .eq(GroupDO::getUsername, UserContext.getUser().getUsername())
                .eq(GroupDO::getDelFlag, 0);
        List<GroupDO> groupDOList = groupMapper.selectList(queryWrapper);

        if (CollUtil.isEmpty(groupDOList)) {
            throw new ClientException("用户无分组信息");
        }

        List<String> gidList = groupDOList.stream().map(GroupDO::getGid).toList();
        requireParam.setGidList(gidList);

        return shortLinkRemoteService.selectPageRecycleBin(requireParam);
    }
}
